package com.zywl.service.impl;

import com.zywl.core.beans.Resource;
import com.zywl.core.dao.mapper.sys.ResourceMapper;
import com.zywl.core.dao.mapper.sys.RoleResRelMapper;
import com.zywl.service.ResourceService;
import com.zywl.service.impl.base.BaseServiceImpl;
import com.zywl.utils.utils.enums.ResTypeEnum;
import com.zywl.utils.utils.enums.YesOrNoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceServiceImpl extends BaseServiceImpl<ResourceMapper, Resource> implements ResourceService {
    @Autowired
    private RoleResRelMapper roleResRelMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Resource t) throws Exception {
//       String status = t.getResStt();
        mapper.updateByPrimaryKey(t);
      /*  long id = t.getResId();
        if (YesOrNoEnum.NO.getKey().equals(status)) {
            mapper.updateUnderResource(id);
            roleResRelMapper.deleteDisabledRel();
        } else if (YesOrNoEnum.YES.getKey().equals(status)) {
            mapper.updateCorrelationResource(id);
        }*/
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) throws Exception {
        mapper.deleteByPrimaryKey(id);
//        roleResRelMapper.deleteByResId(id);
    }

    @Override
    public List<Resource> getAllDataEnable(String order) throws Exception {
        return mapper.getAllDataEnable(order);
    }

    @Override
    public List<Resource> getAllByAssId(Long assId) throws Exception{
        return mapper.getAllByAssId(assId);
    }

    @Override
    public List<Resource> getAllByAssIdAndType(Long assId,String resType) throws Exception{
        return mapper.getAllByAssIdAndType(assId,resType);
    }

    @Override
    public Integer getMaxOrderByPid(Long pId) throws Exception {
        return mapper.getMaxOrderByPid(pId);
    }

    @Override
    public List<Resource> getResources(String resType) throws Exception{
        return mapper.getResources(resType);
    }

    @Override
    public List<Resource> getResourcesByRoleId(Long roleId, String resType) throws Exception {
        return mapper.getResourcesByRoleId(roleId, resType);
    }

    @Override
    public List<Resource> getResourcesMenuByPid(String pId) throws Exception {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("res_p_id", pId);
        condition.put("res_type", ResTypeEnum.MENU.getKey());
        return mapper.getListByParams(condition);
    }

    @Override
    public boolean permissionExist(String permission) throws Exception {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("res_path", permission);
        List<Resource> resources = mapper.getListByParams(condition);
        if (resources != null && !resources.isEmpty()) {
            return true;
        }
        return false;
    }
}
